package com.kjlink.internal.workreport.stats.controller;

import cn.xuqiudong.common.base.controller.BaseController;
import cn.xuqiudong.common.base.lookup.Lookup;
import cn.xuqiudong.common.base.model.BaseResponse;
import com.kjlink.internal.workreport.helper.ReportCommonHelper;
import com.kjlink.internal.workreport.project.model.Project;
import com.kjlink.internal.workreport.stats.lookup.ProjectMonthLookup;
import com.kjlink.internal.workreport.stats.lookup.UserWeekLookup;
import com.kjlink.internal.workreport.stats.model.ProjectTimeStats;
import com.kjlink.internal.workreport.stats.service.ProjectTimeStatsService;
import com.kjlink.internal.workreport.stats.vo.EchartsBarVo;
import com.kjlink.internal.workreport.stats.vo.EchartsSimpleLineVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.time.Period;

/**
 * 功能: :项目工时统计 控制层
 *
 * @author Vic.xu
 * @since 2024-05-27 10:11
 */
@RestController
@RequestMapping("/stats")
public class ProjectTimeStatsController extends BaseController<ProjectTimeStatsService, ProjectTimeStats> {

    @RequestMapping(value = "/", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView index(Project lookup) {
        ModelAndView modelAndView = new ModelAndView("stats/list");
        String endDate = LocalDateTime.now().format(ReportCommonHelper.YEAR_MONTH_FORMAT_FORMATTER);
        String startDate = LocalDateTime.now().minus(Period.ofMonths(1)).format(ReportCommonHelper.YEAR_MONTH_FORMAT_FORMATTER);
        modelAndView.addObject("endDate", endDate);
        modelAndView.addObject("startDate", startDate);
        return modelAndView;
    }

    /**
     * 项目工时柱状图概览
     *
     * @param lookup
     */
    @RequestMapping(value = "/overview", method = {RequestMethod.GET, RequestMethod.POST})
    public BaseResponse<EchartsBarVo> overview(Project lookup) {
        LocalDateTime now = LocalDateTime.now();
        lookup.setSortColumn("usedTime");
        lookup.setSortOrder(Lookup.LookupSortOrder.desc.name());
        EchartsBarVo overview = service.overview(lookup, now.getYear(), now.getMonthValue());

        return BaseResponse.success(overview);
    }

    /**
     * 项目月度工时折线图
     */
    @RequestMapping(value = "/monthLine", method = {RequestMethod.GET, RequestMethod.POST})
    public BaseResponse<EchartsSimpleLineVo> monthLine(ProjectMonthLookup lookup) {
        EchartsSimpleLineVo monthLine = service.monthLine(lookup);
        return BaseResponse.success(monthLine);
    }

    @RequestMapping(value = "/weekBar", method = {RequestMethod.GET, RequestMethod.POST})
    public BaseResponse<EchartsSimpleLineVo> weekBar(UserWeekLookup lookup) {
        return BaseResponse.success(service.weekBar(lookup));
    }

}
